import time
import win32gui
import mss;
import mss.tools
from window_info import WindowInfo
from PIL import ImageGrab

import pyautogui

image_name = 'game'
image_suffix = 'png'

def capture_window(point):
    capture_window3(point)

def capture_window1(point):
    screenshot = ImageGrab.grab(bbox=(point.left, point.top, point.right, point.bottom))
    screenshot.save(image_name + "." + image_suffix)

def capture_window2(point):
    screenshot_region = pyautogui.screenshot(region=(point.left, point.top,point.width,point.height))
    screenshot_region.save(image_name + "." + image_suffix)

def capture_window3(point):
    with mss.mss() as sct:
        # monitor = sct.monitors[0]
        monitor = {"top": point.top, "left": point.left, "width": point.width, "height": point.height}
        img = sct.grab(monitor)
        file_name = image_name + "." + image_suffix
        # 保存图片到文件（可选）
        mss.tools.to_png(img.rgb,img.size, output=file_name)


if __name__ == "__main__":
    point = WindowInfo.getGamePoint()
    win32gui.SetForegroundWindow(point.hwnd)
    time.sleep(0.2)
    print(vars(point))

    count_time = 0
    max = 0
    min = 1000
    count = 1

    for i in range(count):
        start_time = time.perf_counter()
        capture_window(point)
        end_time = time.perf_counter()
        run_time = end_time - start_time
        run_time = round(run_time, 4)
        count_time +=run_time
        if run_time > max:
            max = run_time
        if run_time < min:
            min = run_time

    print("avg:", count_time/count, "seconds")
    print("max:", max, "seconds")
    print("min:", min, "seconds")


